﻿var Dashboard = function(pageContainer) {

    Dashboard.superclass.constructor.call(this, pageContainer);

    var self = this;
    var dashboardManager = null;
    var situatieCO = null;

    this.IsExitPoint = true;
    var isRefreshing = false;

    var Elements = {
        disconnectButton: pageContainer.find(".disconnectButton"),
        dashboardManager: pageContainer.find('.dashboard_manager'),
        situatieCOContainer: pageContainer.find(".situatieCOContainer"),
        horizontalLine: pageContainer.find(".horizontal-line"),
        showEmployees: pageContainer.find(".showEmployees"),
        showSettings: pageContainer.find(".showSettings"),
        showMyProfile: pageContainer.find(".showMyProfile"),
        goToCereriConcediiPage: pageContainer.find(".goToCereriConcediiPage"),
        goToCererePage: pageContainer.find('.goToCererePage'),
        notificari: pageContainer.find('.notificari'),
        showLeftMenu: pageContainer.find(".showLeftMenu")
    };

    self.initialize = override(self.initialize, function() {
        app.Phone.showMenu();
        self.Title = translate('Module_Title_Dashboard');
        initializeDashboardManager();
        initializeSituatieCO();

        Elements.showLeftMenu.tap(function() {
            app.showLeftMenu();
        });

        Elements.showEmployees.click(function(e) {
            if (!app.Pages.EmployeesList.instance)
                app.Pages.EmployeesList.instance = new EmployeesListPage(app.Pages.EmployeesList.container);
            app.changePage(app.Pages.EmployeesList);

            return Event.stopEvent(e);
        });

        Elements.goToCereriConcediiPage.tap(function(e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);
            app.Pages.CereriConcedii.instance.getPullUpData();

            return Event.stopEvent(e);
        });

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(false);
            self.refresh();
        });

        app.translatePage(app.Pages.Dashboard);

        app.updateUnreadNotifications();

        $(document).bind('changedCompany', function() {
            self.refresh();
        });

        $(document).bind('CerereApproved CerereDenied', function() {
            self.refresh();
        });
    });

    var initializeSituatieCO = function() {
        if (app.User.IsAlsoEmployee) {
            if (!situatieCO)
                situatieCO = new SituatieCO(Elements.situatieCOContainer);
            situatieCO.setEmployee({
                Id: app.User.PRA_ID,
                Name: app.User.NumeAngajat
            });
        } else Elements.horizontalLine.hide();
    };

    var initializeDashboardManager = function() {
        Elements.dashboardManager.bind('visible', function() {
            Elements.dashboardManager.show();
            Elements.horizontalLine.show();
        });
        Elements.dashboardManager.bind('notVisible', function() {
            Elements.dashboardManager.hide();
            Elements.horizontalLine.hide();
        });
        if (!dashboardManager)
            dashboardManager = new DashboardManager(Elements.dashboardManager);
    };

    this.refresh = function() {
        if (isRefreshing)
            return;
        isRefreshing = true;

        var loaders = 0;
        if (dashboardManager)
            loaders++;
        if (situatieCO)
            loaders++;

        if (dashboardManager)
            dashboardManager.refresh(function() {
                loaders--;
                hideLoadingScreenOnRefresh(loaders == 0);
            });
        if (situatieCO)
            situatieCO.refresh(false, function() {
                loaders--;
                hideLoadingScreenOnRefresh(loaders == 0);
            });
        $(document).trigger('hidePullDown');
        isRefreshing = false;
    };

    var hideLoadingScreenOnRefresh = function(shouldHide) {
        if (shouldHide)
            isRefreshing = false;
    };

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    this.goBack = function() {
        if (app.Phone)
            app.Phone.exitApplication();
    };

    this.getPullDownData = function() {
        self.refresh();
    };

    self.initialize();
};
extend(Dashboard, TMMobilePage);